/**
 * Created by liweijing on 2016/11/18.
 */

__functionIndexMap = {};

function calliOSFunction(namespace, functionName, args, callback) {
    if (!window.webkit.messageHandlers[namespace]) return;
    var wrap = {
        "method": functionName,
        "params": args
    };
    if (callback) {
        var callbackFuncName;
        if (typeof callback == 'function') {
            callbackFuncName = createCallbackFunction(functionName + "_" + "callback", callback);
        } else {
            callbackFuncName = callback;
        }
        wrap["callback"] = callbackFuncName
    }
    window.webkit.messageHandlers[namespace].postMessage(JSON.stringify(wrap));
}

function createCallbackFunction(funcName, callbackFunc) {
    if (callbackFunc && callbackFunc.name != null && callbackFunc.name.length > 0) {
        return callbackFunc.name;
    }

    if (typeof window[funcName + 0] != 'function') {
        window[funcName + 0] = callbackFunc;
        __functionIndexMap[funcName] = 0;
        return funcName + 0
    } else {
        var maxIndex = __functionIndexMap[funcName];
//        var callbackFuncStr = callbackFunc.toString();
//        for (var i = 0; i <= maxIndex; i++) {
//            var tmpName = funcName + i;
//            if (window[tmpName].toString() == callbackFuncStr){
//                window[tmpName] =  callbackFunc;
//                return tmpName;
//            }
//            
//        }
        var newIndex = ++maxIndex;//++__functionIndexMap[funcName];
        window[funcName + newIndex] = callbackFunc;
        return funcName + newIndex;
    }
}


var kf = {};
kf.getHeadParams = function () {
    // 这个方法特殊处理，有返回值，原生读取的时候之前替换这里的返回值。之后的js方法有返回值的全部用callback方式定义
    return '$headParams';
//    calliOSFunction("kf", "getHeadParams", "",callBackName);
};

kf.checkLoginState = function () {
    // 这个方法特殊处理，有返回值，原生读取的时候之前替换这里的返回值。之后的js方法有返回值的全部用callback方式定义
    return $loginState;
//    calliOSFunction("kf", "checkLoginState","", callBackName);
};

kf.loginFlag = function (isReload) {
    calliOSFunction("kf", "loginFlag", isReload);
};

kf.moreLink = function (link) {
    calliOSFunction("kf", "moreLink", link);
};

kf.close = function () {
    calliOSFunction("kf", "close");
};

kf.openUrlByClient = function (url) {
    calliOSFunction("kf", "openUrlByClient", url);
};

kf.shareByClient = function (shareInfo) {
    calliOSFunction("kf", "shareByClient", shareInfo);
};

kf.findHomeWay = function () {
    calliOSFunction("kf", "findHomeWay");
};

kf.getResourceInfo = function (callBackName) {
    calliOSFunction("kf","getResourceInfo","",callBackName);
};

kf.login = function(callBackName){
    calliOSFunction("kf","login","",callBackName)
}

kf.refresh = function (){
    calliOSFunction("kf","refresh")
};

kf.showMessage = function(message){
    calliOSFunction("kf","showMessage",message)
};

kf.eventListenerCallBack = function(event){
    calliOSFunction("kf","eventListenerCallBack",event)
};

kf.confirm = function(params){
    calliOSFunction("kf","confirm",params)
};

kf.getUserInfo = function(callBackName){
    calliOSFunction("kf","getUserInfo","",callBackName)
};

kf.pay = function(payInfo,callBackName){
    calliOSFunction("kf","pay",payInfo,callBackName)
};

window["kf"] = kf;
